#include "sohu_wanip_api.h"

#define DBG_TAG "api.wanip"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <rtthread.h>

#include "cJSON.h"
#include "gbk2utf8.h"
#include "webclient.h"
#include "http_api_utils.h"

#define SOHU_WANIP_API_URL "http://pv.sohu.com/cityjson"
#define IP_RESPONSE_PREFIX "var returnCitySN ="

int wan_ip_address_get(struct wanip_addr* ip_addr)
{
    char* response = RT_NULL;
    size_t resp_len = 0;

    if (ip_addr == RT_NULL) {
        LOG_E("ip_addr null!");
        return -1;
    }

    if (http_get(SOHU_WANIP_API_URL, &response, &resp_len) < 0) {
        LOG_E("webclient send get request failed.");
        return -RT_ERROR;
    }
    LOG_D("Response length: %d %d", resp_len, rt_strlen(response));

    if (rt_strncmp(response, IP_RESPONSE_PREFIX, rt_strlen(IP_RESPONSE_PREFIX)) == 0) {
        char* json = response + rt_strlen(IP_RESPONSE_PREFIX);
        if (json[resp_len - 1] == ';') {
            json[resp_len - 1] = '\0';
        }

        cJSON* root = cJSON_Parse(json);
        if (root) {
            cJSON* cip = cJSON_GetObjectItem(root, "cip");
            if (cip) {
                rt_strncpy(ip_addr->ip, cip->valuestring, sizeof(ip_addr->ip));
                LOG_D("Got IP: %s", ip_addr->ip);
            }
            cJSON_Delete(root);
        }
    }

    if (response) {
        web_free(response);
    }
    return 0;
}

wanip_t sohu_wanip_get()
{
    char* response = RT_NULL;
    size_t resp_len = 0;

    wanip_t wanip = (wanip_t) rt_calloc(1, sizeof(struct wanip_detail));
    if (wanip == RT_NULL) {
        LOG_E("malloc for wanip_detail failed!");
        return RT_NULL;
    }

    if (http_get(SOHU_WANIP_API_URL, &response, &resp_len) < 0) {
        LOG_E("webclient send get request failed.");
        return RT_NULL;
    }
    LOG_D("Response length: %d %d", resp_len, rt_strlen(response));

    if (rt_strncmp(response, IP_RESPONSE_PREFIX, rt_strlen(IP_RESPONSE_PREFIX)) == 0) {
        char* json_beg = rt_strstr(response, "{");
        char* json_end = rt_strstr(response, ";");
        if (json_beg) {
            *json_end = '\0';
            cJSON* root = cJSON_Parse(json_beg);
            if (root) {
                parse_string_field(root, "cid", &wanip->cid);
                parse_string_field(root, "cip", &wanip->cip);
                parse_string_field(root, "cname", &wanip->cname);
                cJSON_Delete(root);
            }
        }
    }

    if (wanip->cname) {
        char* utf8 = RT_NULL;
        gbk2utf8(&utf8, (void*) wanip->cname, rt_strlen(wanip->cname));
        if (utf8) {
            rt_free((void*) wanip->cname);
            wanip->cname = utf8;
            LOG_I("cname converted to: %s", utf8);
        }
    }

    if (response) {
        web_free(response);
    }
    return 0;
}

void sohu_wanip_put(wanip_t wanip)
{
    if (wanip == RT_NULL) {
        LOG_E("wanip null!");
        return;
    }

    SAFE_FREE(wanip->cid);
    SAFE_FREE(wanip->cip);
    SAFE_FREE(wanip->cname);
    SAFE_FREE(wanip);
}

static int ip_test(int argc, char* argv[])
{
    struct wanip_addr ip_addr = {0};
    if (wan_ip_address_get(&ip_addr) < 0) {
        LOG_E("get ip failed!");
        return -1;
    }
    return 0;
}
MSH_CMD_EXPORT(ip_test, IP API test);